﻿using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel.Extensions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.Material
{
    /// <summary>
    /// 计划物料需求创建视图模型
    /// </summary>
    public class AddPlanMaterialAskViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 资源定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IResourceContract> _resourceContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public AddPlanMaterialAskViewModel(ServiceProxy<IResourceContract> resourceContract)
        {
            this._resourceContract = resourceContract;
        }

        #endregion

        #region # 属性

        #region 已选物料定义 —— MaterialInfo SelectedMaterial
        /// <summary>
        /// 已选物料定义
        /// </summary>
        [DependencyProperty]
        public MaterialInfo SelectedMaterial { get; set; }
        #endregion

        #region 数量 —— decimal? Quantity
        /// <summary>
        /// 数量
        /// </summary>
        [DependencyProperty]
        public decimal? Quantity { get; set; }
        #endregion

        #region 单位 —— string Unit
        /// <summary>
        /// 单位
        /// </summary>
        [DependencyProperty]
        public string Unit { get; set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 物料定义列表 —— ObservableCollection<MaterialInfo> Materials
        /// <summary>
        /// 物料定义列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<MaterialInfo> Materials { get; set; }
        #endregion

        #endregion

        #region # 方法

        //Initializations

        #region 初始化 —— override async Task OnInitializeAsync(CancellationToken cancellationToken)
        /// <summary>
        /// 初始化
        /// </summary>
        protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
        {
            IEnumerable<MaterialInfo> materialInfos = await Task.Run(() => this._resourceContract.Channel.GetMaterialsByPage(null, null, null, null, true, null, 1, int.MaxValue).Datas, cancellationToken);
            this.Materials = new ObservableCollection<MaterialInfo>(materialInfos);
        }
        #endregion


        //Actions

        #region 加载单位 —— void LoadUnit()
        /// <summary>
        /// 加载单位
        /// </summary>
        public void LoadUnit()
        {
            #region # 验证

            if (this.SelectedMaterial == null)
            {
                this.Unit = null;
                return;
            }

            #endregion

            this.Unit = this.SelectedMaterial.StandardUnitNo;
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            if (this.SelectedMaterial == null)
            {
                MessageBox.Show("物料定义不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (this.Quantity == null)
            {
                MessageBox.Show("数量不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            #endregion

            await base.TryCloseAsync(true);
        }
        #endregion

        #endregion
    }
}
